/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package competition.JPA;

import competition.business.AbstractEvent;
import competition.*;
import competition.DAO.AbstractEventDAO;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author Administrateur
 */
public class JPAEvent implements AbstractEventDAO {

    public void CreateEvent(AbstractEvent NewEvent) {
        try{
            JpaUtil.getTransactionEntityManager().begin();
            JpaUtil.getEntityManager().persist(NewEvent);
            JpaUtil.getTransactionEntityManager().commit();
        }catch(Exception e){ e.printStackTrace(); }
    }

    public void UpdateEvent(AbstractEvent NewEvent) {
        try{
            JpaUtil.getTransactionEntityManager().begin();
            JpaUtil.getEntityManager().refresh(NewEvent);
            JpaUtil.getTransactionEntityManager().commit();
        }catch(Exception e){ e.printStackTrace(); }
    }
    
    public void DeleteEvent(AbstractEvent NewEvent) {
        try{
            JpaUtil.getTransactionEntityManager().begin();
            JpaUtil.getEntityManager().remove(NewEvent);
            JpaUtil.getTransactionEntityManager().commit();
        }catch(Exception e){ e.printStackTrace(); }
    }

    public List<AbstractEvent> GetEventByDuration(int TimeSpan) {
        Query query = JpaUtil.getEntityManager().createQuery("SELECT evnt FROM AbstractEvent AS evnt WHERE evnt.duration = '" + Integer.toString(TimeSpan) + "'");
        return query.getResultList();
    }

    public AbstractEvent GetEventByName(String Name) {
        Query query = JpaUtil.getEntityManager().createQuery("SELECT evnt FROM AbstractEvent AS evnt WHERE evnt.name = '" + Name + "'");
        return (AbstractEvent)query.getSingleResult();
    }

    public List<AbstractEvent> GetEventByType(String Type) {
        Query query = JpaUtil.getEntityManager().createQuery("SELECT evnt FROM AbstractEvent AS evnt WHERE evnt.type = '" + Type + "'");
        return query.getResultList();
    }

    public List<AbstractEvent> GetAllEvents() {
        Query query = JpaUtil.getEntityManager().createQuery("SELECT evnt FROM AbstractEvent AS evnt");
        return query.getResultList();
    }
}
